<?php
class User extends CI_Controller
{

	private $user_id;
	
	private $min_username;
	private $max_username;
	private $min_password;
	private $max_password;
	
	function __construct()
	{
		parent::__construct();
		$this->min_username = MIN_USERNAME;
		$this->max_username = MAX_USERNAME;
		$this->min_password = MIN_PASSWORD;
		$this->max_password = MAX_PASSWORD;
//		$this->output->enable_profiler(TRUE);

		$this->load->library('form_validation');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('City_m','city');
		
		$is_login = $this->dx_auth->is_logged_in();
		if(!$is_login){
			redirect('', 'location');
		}
		$this->user_id = $this->dx_auth->get_user_id();
	}

	function index()
	{
		$this->tags();
	}

	function tags(){
		
		$this->load->model('Category_m','category');
		
		$ispost = $this->input->post('ispost');
		if($ispost){
			$tags = $this->input->post('tag');
			if(!$tags){
				$tags = array();
			}
			$this->category->set_user_category($this->user_id, $tags);
		}
		
		
		$all_cat = $this->category->get_user_all_category_status($this->user_id);
		
		$data = array(
					'type' => 'tags',
					'all_cat' => $all_cat,
					'is_config' => true
					);
		$this->load->view('user/user',$data);
	}
	
	function site(){
		
		$this->load->model('Site_m','site');
		
		$ispost = $this->input->post('ispost');
		if($ispost){
			$site = $this->input->post('site');
			if(!$site){
				$site = array();
			}
			$this->site->set_user_sites($this->user_id, $site);
		}
		
		$all_site = $this->site->get_all_site();
		$user_site = $this->site->get_user_valid_site($this->user_id);
		
		$data = array(
					'type' => 'site',
					'all_site' => $all_site,
					'user_site' => $user_site,
					'is_config' => true
					);
		$this->load->view('user/user',$data);
	}
	
	function city(){
		$ispost = $this->input->post('ispost');
		if($ispost){
			$city = $this->input->post('city');
			$city_id = intval($city);
			if($city_id){
				$city_name = $this->city->get_city_by_id($city_id);
				if($city_name){
					$this->load->model('dx_auth/user_profile', 'user_profile');
					$this->user_profile->set_profile($this->user_id,array('city_id' => $city_id));
					$this->city->set_city_info_in_session($city_id,$city_name);
				}
			}
		}
		
		$city_id = $this->session->userdata('city_id');
		$all_city = $this->city->get_all();
		$data = array(
					'type' => 'city',
					'city_id' => $city_id,
					'all_city' => $all_city					
					);
		$this->load->view('user/user',$data);
	}
	
	function mail(){
		
		$this->load->model('dx_auth/user_profile', 'user_profile');
		
		$ispost = $this->input->post('ispost');
		if($ispost){
			$ismail = $this->input->post('sendmail');
			if(is_numeric($ismail)){
				$this->user_profile->set_profile($this->user_id,array('is_mail_send' => $ismail));
			}
		}
		
		$ismail = $this->user_profile->get_profile_field($this->user_id,'is_mail_send');
		$data = array(
					'type' => 'mail',
					'is_mail' => $ismail
					);
		$this->load->view('user/user',$data);
	}
	
	function changemail(){

		$val = $this->form_validation;
		// Set form validation
		$val->set_rules('email', '邮箱地址', 'trim|required|xss_clean|valid_email|callback_email_check');	
		
		$data = array(
					'type' => 'changemail',
					'auth_message' => ''
					);
		// Validate rules and change password
		if ($val->run() AND $this->dx_auth->change_tuan_username($this->user_id, $val->set_value('email')))
		{
			$data['auth_message'] = '您的邮箱更改成功';			
		}

		$this->load->view('user/user',$data);
	}
	
	function changepwd(){
		$ispost = $this->input->post('ispost');
		$val = $this->form_validation;
		// Set form validation
		$val->set_rules('old_password', '旧密码', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']');
		$val->set_rules('new_password', '新密码', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']|matches[confirm_new_password]');
		$val->set_rules('confirm_new_password', '确认密码', 'trim|required|xss_clean');
		
		$data = array(
					'type' => 'changepwd',
					'auth_message' => $ispost?'密码更新失败':'',
					'old_pass_check' => ''
					);
		// Validate rules and change password
		if ($val->run()){
			if($this->dx_auth->change_password($val->set_value('old_password'), $val->set_value('new_password'))){
				$data['auth_message'] = '您的密码已成功更新';	
			}else{
				$data['old_pass_check'] = '密码错误';
			}
		}
				
		$this->load->view('user/user',$data);
	}
	
	function email_check($email)
	{
		if($email === $this->dx_auth->get_username()){
			$this->form_validation->set_message('email_check', '与现在的电子邮箱地址一致');
			return false;
		}
		
		$result = $this->dx_auth->is_email_available($email);
		if ( ! $result)
		{
			$this->form_validation->set_message('email_check', '电子邮箱地址已存在');
		}

		return $result;
	}
}
?>